<template>
  <div>
      <div class="myRow">
        <div>
          <i-button class="myButton" type="info" ghost icon="md-add-circle"  @click="modal = true">新增模块</i-button>
          <i-button :disabled="selectList.length === 0" class="myButton" type="error" ghost icon="md-trash" @click="deleteList()">批量删除</i-button>
        </div>
        <div>
          <Input v-model="searchData" icon="ios-search" @on-click="getListFun" @on-enter="getListFun" placeholder="搜索内容" style="width: auto" />
        </div>
      </div>
      <i-table :loading="loading" border ref="selection" :columns="columns4" :data="listData.listT"  @on-selection-change="selectChange"></i-table>
      <div class="footPage">
        <Page @on-change="changePage" :total="listData.recordCount" :page-size="listData.pageSize" :current="pageIndex"/>
      </div>
      <Modal
        v-model="modal"
        title="新增模块">
        <div id="box">
          <i-form :model="addData" label-position="right" ref="addData" :rules="ruleValidate" :label-width="100">
            <FormItem label="模块名称：" prop="modularName">
                <i-input v-model="addData.modularName"></i-input>
            </FormItem>
            <FormItem label="模块标识：" prop="modularCode">
              <i-input v-model="addData.modularCode"></i-input>
            </FormItem>
            <FormItem label="真实路径：" prop="actualPath">
              <i-input v-model="addData.actualPath"></i-input>
            </FormItem>
            <FormItem label="显示路径：" prop="showUrl">
                <i-input v-model="addData.showUrl"></i-input>
            </FormItem>
          </i-form>
        </div>
        <div slot="footer">
            <i-button type="text" :loading="modal_loading" @click="reset('addData')">重置</i-button>
            <i-button type="primary" :loading="modal_loading" @click="ok('addData')">确定</i-button>
        </div>
    </Modal>
    <Modal
        v-model="modal2"
        title="修改模块">
        <div id="box">
          <i-form :model="editData" label-position="right" ref="editData" :rules="ruleValidate" :label-width="100">
            <FormItem label="模块名称：" prop="modularName">
                <i-input v-model="editData.modularName"></i-input>
            </FormItem>
            <FormItem label="模块标识：" prop="modularCode">
              <i-input v-model="editData.modularCode"></i-input>
            </FormItem>
            <FormItem label="真实路径：" prop="actualPath">
              <i-input v-model="editData.actualPath"></i-input>
            </FormItem>
            <FormItem label="显示路径：" prop="showUrl">
                <i-input v-model="editData.showUrl"></i-input>
            </FormItem>
            <FormItem label="模块排序：" prop="modularOrder">
              <InputNumber :min="0" v-model="editData.modularOrder" :editable="false"></InputNumber>
            </FormItem>
          </i-form>
        </div>
        <div slot="footer">
            <i-button type="text" :loading="modal_loading" @click="reset2('editData')">重置</i-button>
            <i-button type="primary" :loading="modal_loading" @click="editFun('editData')">保存</i-button>
        </div>
    </Modal>
  </div>
</template>

<script>
import {
  AddPermissionModular,
  GetPermissionModularPager,
  UpdatePermissionModular,
  UpdatePermissionModularByIdsOrId,
  GetPermissionModularAllInfo
} from "@/api/permissionModular";
export default {
  name: "home",
  components: {},
  data() {
    return {
      ruleValidate: {
        modularName: [
          {
            required: true,
            message: "请输入模块名称",
            trigger: "blur"
          }
        ],
        modularCode: [
          {
            required: true,
            message: "请输入模块标识",
            trigger: "blur"
          }
        ],
        actualPath: [
          {
            required: true,
            message: "请输入真实路径",
            trigger: "blur"
          }
        ],
        // modularOrder: [
        //   {
        //     required: true,
        //     message: "请输入模块顺序",
        //     trigger: "blur"
        //   }
        // ],
        showUrl: [
          {
            required: true,
            message: "请输入显示路径",
            trigger: "blur"
          }
        ]
      },
      selectList: [], // 多选列表
      searchData: "",
      roleListData: [], // 模块列表
      selectStr: "", // 选中列表id字符串
      pageIndex: 1, // 当前页
      modal_loading: false,
      addData: {
        modularName: "",
        modularCode: "",
        actualPath: "",
        showUrl: "",
        isDelete: 0
      },
      editData: {
        modularName: "",
        modularCode: "",
        actualPath: "",
        showUrl: "",
        modularOrder: 0,
        isDelete: 0
      },
      social: [], // 模块页面列表
      addPermissionsSetting: "", // 模块页面列表字符串
      editPermissionsSetting: "", // 修改列表字符串
      editId: "",
      modal: false,
      modal2: false,
      loading: false,
      columns4: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "ID",
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "模块名称",
          key: "modularName",
          align: "center"
        },
        {
          title: "模块标识",
          key: "modularCode",
          align: "center"
        },
        {
          title: "真实路径",
          key: "actualPath",
          align: "center"
        },
        {
          title: "显示路径",
          key: "showUrl",
          align: "center"
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.deleteBtn(params);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      listData: {}
    };
  },
  mounted() {
    this.getListFun();
    this.getRoleList();
  },
  methods: {
    instance(confirmationBoxContent) {
      const title = confirmationBoxContent.title;
      const content = confirmationBoxContent.contentText;
      this.$Modal.confirm({
        title: title,
        content: content,
        onOk: () => {
          this.deleteFun(confirmationBoxContent.params);
        },
        onCancel: () => {
          this.$Message.info("操作取消");
        }
      });
    },
    // 批量删除
    deleteList() {
      var newStr = "";
      for (var i = 0; i < this.selectList.length; i++) {
        if (newStr === "") {
          newStr = this.selectList[i].id;
        } else {
          newStr = newStr + "," + this.selectList[i].id;
        }
      }
      var confirmationBoxContent = {
        title: "确认删除",
        contentText: "一旦删除不可撤销，确认删除？",
        params: newStr.toString()
      };
      this.instance(confirmationBoxContent);
    },
    // 获取模块页面列表
    getRoleList() {
      GetPermissionModularAllInfo()
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "roleListData", res.data.result);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 获取用户列表数据
    getListFun() {
      this.$set(this, "loading", true);
      const p = {
        pageIndex: this.pageIndex,
        pageSize: "10",
        keyWord: this.searchData
      };
      GetPermissionModularPager(p)
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "listData", res.data.result);
          }
          this.$set(this, "loading", false);
        })
        .catch(err => {
          console.log(err);
          this.$set(this, "loading", false);
        });
    },
    // 新增接口
    addFun() {
      const p = {
        modularName: this.addData.modularName,
        modularCode: this.addData.modularCode,
        actualPath: this.addData.actualPath,
        showUrl: this.addData.showUrl,
        isDelete: 0
      };
      AddPermissionModular(p)
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "addData", {
              modularName: "",
              modularCode: "",
              actualPath: "",
              showUrl: "",
              isDelete: 0
            });
            this.$set(this, "addPermissionsSetting", "");
            this.$set(this, "social", []);
            this.getListFun();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 分页回调方法
    changePage(index) {
      this.$set(this, "pageIndex", index);
      this.getListFun();
    },
    ok(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.addFun();
          this.$set(this, "modal", false);
        } else {
          this.$Message.error("提交内容有误！请检查");
        }
      });
    },
    editFun(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          const p = {
            modularName: this.editData.modularName,
            modularOrder: this.editData.modularOrder,
            modularCode: this.editData.modularCode,
            actualPath: this.editData.actualPath,
            showUrl: this.editData.showUrl,
            isDelete: 0,
            id: this.editId
          };
          UpdatePermissionModular(p)
            .then(res => {
              if (res.data.code === "0") {
                this.$set(this, "editData", {
                  modularName: "",
                  modularCode: "",
                  actualPath: "",
                  showUrl: "",
                  modularOrder: 0,
                  isDelete: 0
                });
                this.getListFun();
              }
            })
            .catch(err => {
              console.log(err);
            });
          this.$set(this, "modal2", false);
        } else {
          this.$Message.error("提交内容有误！请检查");
        }
      });
    },
    edit(params) {
      debugger;
      this.$set(this, "editData", {
        modularName: params.row.modularName,
        modularCode: params.row.modularCode,
        modularOrder: params.row.modularOrder,
        actualPath: params.row.actualPath,
        showUrl: params.row.showUrl,
        id: this.editId
      });
      this.$set(this, "editPermissionsSetting", params.row.permissionsSetting);
      this.$set(this, "editId", params.row.id);
      this.$set(this, "modal2", true);
    },
    deleteFun(ids) {
      const p = {
        idsStr: ids,
        isDelete: 1
      };
      UpdatePermissionModularByIdsOrId(p)
        .then(res => {
          if (res.data.code === "0") {
            this.getListFun();
            this.$set(this, "selectList", []);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    deleteBtn(params) {
      var confirmationBoxContent = {
        title: "确认删除",
        contentText: "一旦删除不可撤销，确认删除？",
        params: params.row.id.toString()
      };
      this.instance(confirmationBoxContent);
    },
    reset(name) {
      this.$refs[name].resetFields();
      this.$set(this, "addData", {
        modularName: "",
        modularCode: "",
        actualPath: "",
        showUrl: "",
        isDelete: 0
      });
      this.$set(this, "addPermissionsSetting", "");
      this.$set(this, "social", []);
    },
    reset2(name) {
      this.$refs[name].resetFields();
      this.$set(this, "editData", {
        modularName: "",
        modularCode: "",
        actualPath: "",
        showUrl: "",
        modularOrder: 0,
        isDelete: 0
      });
    },
    selectChange(list) {
      this.$set(this, "selectList", list);
    }
  }
};
</script>

<style lang="less">
.textCenter {
  text-align: center;
}
.myRow {
  width: 100%;
  margin: 0px 0px 10px;
  display: flex;
  justify-content: space-between;
}
.myButton {
  margin-right: 6px;
}
.footPage {
  width: 100%;
  margin-top: 20px;
  text-align: center;
}
</style>
